]> git.r.bdr.sh - rbdr/captura/blame_incremental - Captura/Core Extensions/CGImage+resize.swift
Add sparkle
[rbdr/captura] / Captura / Core Extensions / CGImage+resize.swift
... / ...
CommitLineData
1import CoreGraphics
2
3extension CGImage {
4 func resize(by scale: CGFloat) -> CGImage? {
5 let width = Int(CGFloat(self.width) / scale)
6 let height = Int(CGFloat(self.height) / scale)
7
8 let bitsPerComponent = self.bitsPerComponent
9 let colorSpace = self.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!
10 let bitmapInfo = self.bitmapInfo.rawValue
11
12 guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo) else {
13 return nil
14 }
15
16 context.interpolationQuality = .high
17 context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
18
19 return context.makeImage()
20 }
21}